package cn.bj.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.bj.beans.Yxzc_work_picture;
import cn.bj.service.YxzcWorkPictureService;

/*
 * 优选婚庆作品集控制层
 * 
 * */
@Controller
@RequestMapping("/admin")
public class YxzcWorkPictureController {
	@Autowired
	YxzcWorkPictureService yxzcWorkPictureService;

	// 查询所有
	/*
	 * @RequestMapping("/") public String getAllYxhqWorkPicture (){
	 * List<Yxhq_work_picture> yxhqWPs =
	 * yxhqWorkPictureService.getAllYxhqWorkPicture(); return ""; }
	 */

	// 增加
	@ResponseBody
	@PostMapping(value = "/insertYxzcWP.do") // 参数放入MultipartFile接口，引用放入jsp页面上传文件的name属性
	public String insertYxzcWP(Yxzc_work_picture yxzcWP, HttpServletRequest request, MultipartFile fd2,
			HttpSession session) throws IllegalArgumentException, IOException {

		// 返回信息
		String msg;

		if (null != fd2) {
			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/YxzcImg");

			// 文件名以原来文件名命名
			String ImgName = fd2.getOriginalFilename();

			// 限制上传的文件名的后缀名为jpg、png
			if ((ImgName.endsWith("jpg") || ImgName.endsWith("png"))) {
				// 装载文件
				File file = new File(path, ImgName);

				// 上传文件
				fd2.transferTo(file);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/YxzcImg/" + ImgName;

				// 创建
				yxzcWP.setPicture(path);

				int num4 = yxzcWorkPictureService.insertYxzcWp(yxzcWP);
				if (num4 != 0) {
					msg = "添加成功";
					System.out.println(msg);
					return msg;
				} else {
					msg = "添加失败";
					return msg;
				}

			}

			msg = "图片格式不正确，请确认图片格式jpg/png";
			return msg;
		}
		msg = "文件不能为空";
		return msg;
	}

	// 删除
	@RequestMapping("/deleteYxzcWP.do")
	public String deleteYxhqWP(@Param("id") Integer id) {
		int num5 = yxzcWorkPictureService.deleteByYxhqWP(id);
		System.out.println(num5);
		return "redirect:/admin/AllAdmins.do";
	}

	// 跳转修改图片,拿到单个对象
	@RequestMapping("/GotoUpdateYxzcWP.do")
	public String GotoUpdatePicture(@Param("id") Integer id, Model model) {
		Yxzc_work_picture yzp = yxzcWorkPictureService.selectById(id);
		model.addAttribute("yzp", yzp);
		return "admin/updateYxzcWP";
	}

	// 修改
	@PostMapping(value = "/YxzcWPUpdate.do") // 参数放入MultipartFile接口，引用放入jsp页面上传文件的name属性
	public String updateYxhqWP(Yxzc_work_picture yzp, HttpServletRequest request, MultipartFile img,
			HttpSession session) throws IllegalArgumentException, IOException {

		if (null != img) {
			// 得到上传文件的的存储路径，web项目一般只能存储在自己的项目根下，因为服务器是租的别人的
			String path = session.getServletContext().getRealPath("/YxzcImg");

			// 文件名以原来文件名命名
			String ImgName = img.getOriginalFilename();

			// 限制上传的文件名的后缀名为jpg、png
			if ((ImgName.endsWith("jpg") || ImgName.endsWith("png"))) {
				// 装载文件
				File file = new File(path, ImgName);

				// 上传文件
				img.transferTo(file);

				// 拼凑服务器图片的路径
				path = session.getServletContext().getContextPath() + "/YxzcImg/" + ImgName;

				// 创建
				yzp.setPicture(path);

				int num4 = yxzcWorkPictureService.updateYxzcWp(yzp);
				if (num4 != 0) {
					return "redirect:/admin/AllAdmins.do";
				} else {
					return "admin/error2";
				}
			}
			return "admin/error2";
		}
		return "admin/error2";
	}

}
